package net.melon.cata.manager.id;

import net.esj.basic.utils.Assert;
import net.melon.cata.manager.exception.OverMaxCountException;
import net.melon.cata.pojo.Catalog;

/**
 * 十进制ID生成规则：
 * child = parent*100+1
 * 每层99个
 * @author Administrator
 *
 */
public class DecimalIDGenerate implements ScaleIDGenerate {

	@Override
	public synchronized Long generateFirstChild(Long parentId) {
		Assert.notNull(parentId);
		Long id = parentId*100L+1;
		return id;
	}

	@Override
	public Long getParent(Long childId) {
		Assert.notNull(childId);
		Long id = childId/100L;
		return id;
	}

	@Override
	public synchronized Long generateBrother(Long brotherId, Catalog catalog)throws OverMaxCountException{
		Assert.notNull(brotherId);
		Long currentSerial = brotherId%100;
		if(currentSerial==99){
			throw new OverMaxCountException("over max count : 100 ");
		}else{
			Long ret = brotherId+1;
			if(catalog==null || ret!=10){
				return ret;
			}else{
				return catalog.getId()*100+ ret;
			}
		}
	}

}
